<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="utf-8">
    <title>资源分类列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" media="all">

    <style>
        .flag-span {
            display:inline-block;
            height:25px;
            line-height:25px;
            padding:0 8px;
            border:1px solid transparent;
            color:#fff;
            text-align:center;
            font-size:12px;
            border-radius:2px;
        }
        .flag-span-green {
            background-color:#5FB878;
        }
        .flag-span-orange {
            background-color:#fe9c5b;
        }
        .flag-span-grey {
            background-color:#d3d3d3;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">资源名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" placeholder="请输入资源名" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">分类类型</label>
                            <div class="layui-input-inline">
                                <div id="oneType" class="xm-select-demo"></div>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="oneTypeName">
            <span class="flag-span flag-span-green"> {{ d.oneTypeName }} </span>
        </script>

        <script type="text/html" id="twoTypeName">
            {{# if (d.twoTypeName == "无分类") { }}
            <span class="flag-span flag-span-grey"> {{ d.twoTypeName }} </span>
            {{# } else { }}
            <span class="flag-span flag-span-orange"> {{ d.twoTypeName }} </span>
            {{# } }}
        </script>

        <script type="text/html" id="cover">
            <img src="{{ d.cover }}">
        </script>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script th:src="@{/layuimini/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script th:src="@{/layuimini/lib/jquery-3.4.1/jquery-3.4.1.min.js}"></script>
<script th:src="@{/layuimini/js/lay-config.js}" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'xmSelect'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            xmSelect = layui.xmSelect;

        $.post('/admin/resource-type/selectParentNames', function (res) {
            var oneTypeData = res.data.resourceTypes
            xmSelect.render({
                el: '#oneType',
                tips: '请选择一级分类',
                name: 'oneTypeId',
                radio: true,
                clickClose: true,
                model: {
                    label: {
                        type: 'text',
                        text: {
                            //左边拼接的字符
                            left: '',
                            //右边拼接的字符
                            right: '',
                            //中间的分隔符
                            separator: ', ',
                        },
                    }
                },
                data: oneTypeData
            })
        })

        //表格方法
        function tableRender() {
            table.render({
                elem: '#currentTableId',
                // url: '/layuimini/api/resource.json',
                url: '/admin/resource/selectResourceList',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                parseData: function (res) { //res 即为原始返回的数据
                    return {
                        "code": res.code,       //解析接口状态
                        "msg": res.msg,         //解析提示文本
                        "count": res.count,     //解析数据长度
                        "data": res.data.resources  //解析数据列表
                    };
                },
                cols: [
                    [
                        {type: "checkbox", width: 50},
                        {field: 'id', width: 80, title: 'ID', align: "center"},
                        {field: 'name', width: 100, title: '资源名', align: "center"},
                        {field: 'oneTypeName', width: 110, title: '一级分类', templet: "#oneTypeName", align: "center"},
                        {field: 'twoTypeName', width: 110, title: '二级分类', templet: "#twoTypeName", align: "center"},
                        {field: 'description', width: 180, title: '介绍', align: "center"},
                        {field: 'link', width: 240, title: '链接', align: "center"},
                        {field: 'cover', width: 180, title: '图示', align: "center", templet: "#cover"},
                        {field: 'createTime', width: 170, title: '创建时间', align: "center"},
                        {field: 'updateTime', width: 170, title: '修改时间', align: "center"},
                        {title: '操作', minWidth: 140, toolbar: '#currentTableBar', align: "center", fixed: 'right'}
                    ]
                ],
                limits: [10, 25, 50, 100],
                limit: 10,
                page: true,
                skin: 'line'
            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                var result = data.field

                //执行搜索重载
                table.reload('currentTableId', {
                    page: {
                        curr: 1
                    }
                    , where: result
                }, 'data');

                return false;
            });
        }

        //展示表格
        tableRender()

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '分类添加',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/resource/resource-add',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                var checkCount = checkStatus.data.length
                if (checkCount === 0) {
                    layer.msg('批量删除至少选中一项数据');
                    return false;
                }
                layer.confirm('真的要删除选中的项吗？',function() {
                    var idArrays = [];
                    for (var i = 0; i < checkCount; i++) {
                        idArrays.push(checkStatus.data[i].id)
                    }
                    var ids = ""
                    idArrays.forEach(function (value) {
                        ids += value + ","
                    })
                    ids = ids.substring(0, ids.length - 1)
                    $.get('/admin/resource/deleteResources/' + ids, function (res) {
                        if (res.code === 200) {
                            //刷新表格
                            tableRender()
                            layer.msg(res.msg)
                        } else if (res.code === 444) {
                            layer.msg(res.msg)
                        }
                    })
                })
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '分类修改',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/resource/resource-edit/' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function () {
                    $.get('/admin/resource/deleteResource/' + data.id, function (res) {
                        if (res.code === 200) {
                            tableRender()
                            layer.msg(res.msg)
                        } else if (res.code === 444) {
                            layer.msg(res.msg)
                        }
                    })
                });
            }
        });

    });
</script>

</body>
</html>
